import router from '@/router'

// 定义一个公共模块来管理用户的信息
const userModule = (() => {
  let userInfo = null // 用户信息

  const getUserInfo = () => {
    let userInfoStr = null
    const path = router.currentRoute.path
    if (path.includes('/admin')) {
      userInfoStr = localStorage.getItem('a_u')
    } else {
      userInfoStr = localStorage.getItem('u_u')
    }
    if (userInfoStr) {
      userInfo = JSON.parse(userInfoStr)
    } else {
      userInfo = null
    }
  }

  const setUserInfo = (data) => {
    userInfo = data
    router.currentRoute.path.includes('/admin')
      ? localStorage.setItem('a_u', JSON.stringify(data))
      : localStorage.setItem('u_u', JSON.stringify(data))
  }

  return {
    getUser() {
      getUserInfo()
      return userInfo
    },

    setUser(data) {
      setUserInfo(data)
    }
  }
})()

export default userModule
